package com.lzyyj.gmis.base.dao.ibatis;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import com.lzyyj.common.GenericsUtils;
import com.lzyyj.common.Page;
import com.lzyyj.gmis.base.dao.IBaseDao;

/** 
 * Hibernate Dao 的泛型基类. 
 * <p/> 
 * 继承于Spring的<code>HibernateDaoSupport</code>,提供分页函数和若干便捷查询方法，并对返回值作了泛型类型转换. 
 * 
 * @author lzyyj 
 * @see HibernateDaoSupport 
 * @see HibernateEntityDao 
 */ 
@Transactional
@SuppressWarnings("unchecked") 
public class IbatisEntityDao<T,PK extends Serializable> extends SqlMapClientDaoSupport implements IBaseDao<T,PK> {
	
	private static final Log log = LogFactory.getLog(IbatisEntityDao.class);
		
	private static Map<Class<?>, IbatisEntityDao<?, Serializable>> daoMap = new HashMap<Class<?>, IbatisEntityDao<?, Serializable>>();
	
	protected Class<T> clazz;	// DAO所管理的Entity类型. 
	
	/**
	 * 默认构造函数, 通过继承关系确定管理的实体类
	 */
	public IbatisEntityDao(){
        clazz = (Class<T>) GenericsUtils.getSuperClassGenricType(getClass()); 
        log.info("初始化1 HibernateDao:"+(clazz==null?"?":clazz.getName())+"");
	}
	
	/**
	 * 带参构造函数，构造时注入管理的实体类
	 * @param clazz
	 */
	public IbatisEntityDao(Class<T> clazz){
		this.clazz = clazz; 
        log.info("初始化2 HibernateDao:"+(clazz==null?"?":clazz.getName())+"");
	}
	
	public void setEntityClass(Class<T> clazz) throws Exception{
		if (this.clazz==null){
			this.clazz = clazz;
			daoMap.put(clazz, (IbatisEntityDao<?, Serializable>)this);
		} else {
			if (this.clazz!=clazz){
				log.warn("中途更改实体类型，不推荐！"+this.clazz.getSimpleName()+" 改为 "+clazz.getSimpleName()+"");
				throw new Exception("中途更改实体类型，不推荐！"+this.clazz.getSimpleName()+" 改为 "+clazz.getSimpleName()+"");
			}
		} 
		this.clazz = clazz ;
	}

	public T save(T entity) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(Object obj) throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void deleteById(PK id) {
		// TODO Auto-generated method stub
		
	}

	public void deleteByIds(PK[] ids) {
		// TODO Auto-generated method stub
		
	}

	public T update(T entity) {
		// TODO Auto-generated method stub
		return null;
	}

	public T saveOrUpdate(T entity) {
		// TODO Auto-generated method stub
		return null;
	}

	public void executeUpdate(String jpql, Object... values) {
		// TODO Auto-generated method stub
		
	}

	public T getNew() {
		// TODO Auto-generated method stub
		return null;
	}

	public T get(T entity) {
		// TODO Auto-generated method stub
		return null;
	}

	public T getById(PK id) {
		// TODO Auto-generated method stub
		return null;
	}

	public T load(PK id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public long getAllCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getCount(Map<String, Object> condition) {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getCount(String condition) {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean isUnique(T entity, String uniquePropertyNames) {
		// TODO Auto-generated method stub
		return false;
	}

	public T findUniqueBy(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	public T findUniqueBy(String propertyList, Object... values) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findAll(String orderBy, long... firstAndMaxResults) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findAll(String fieldList, String orderFields, long... rowStartIdxAndCount) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findByExample(T instance, long... firstAndMaxResults) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findByExample(T entity, String propertyList, String orderBy, long... firstAndMaxResults)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findByProperty(String propertyName, Object value, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findByProperty(String propertyName, Object value, String orderBy, long... firstAndMaxResults) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<T> findByProperty(Page<T> page, String propertyName, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> find(String hql, Object... values) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page<T> find(Page<T> page) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findBy(String condition, long startIndex, long maxCount, String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	public String getPKName() {
		// TODO Auto-generated method stub
		return null;
	}

	public void flush() {
		// TODO Auto-generated method stub
		
	}

	public void clear() {
		// TODO Auto-generated method stub
		
	}

	public long getCount(Page<T> page) {
		// TODO Auto-generated method stub
		return 0;
	}

	public long getCountByProperty(String propertyName, Object value, String orderBy) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void merge(T entity) {
		// TODO Auto-generated method stub
		
	}

	public PK getPK(T entity) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		// TODO Auto-generated method stub
		return null;
	}

	public Query createQuery(String hql, Object... values) {
		// TODO Auto-generated method stub
		return null;
	}

	public void callProc(String procName, Object... values) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	public String getPropertyList() {
		// TODO Auto-generated method stub
		return null;
	}

	public String[] getProperties() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isEntity(Class<?> clazz) {
		// TODO Auto-generated method stub
		return false;
	}

	public Object getByClass(Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Map<String, Object>> getEntityNameList() {
		// TODO Auto-generated method stub
		return null;
	}

	public String[] getProperties(Class<?> entityClass) {
		// TODO Auto-generated method stub
		return null;
	}

	public <E> List<E> findByProperty(Class<E> entityClass, String propertyName, Object value, String orderBy,
			long... firstAndMaxResults) {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, List<? extends Object>> getRefListMap(Class<?> objectClass, String aliasNames, Serializable objId)
			throws ClassNotFoundException {
		// TODO Auto-generated method stub
		return null;
	}

	public <T1, PK1 extends Serializable> IBaseDao<T1, PK1> getBaseDao(Class<T1> clazz1) {
		// TODO Auto-generated method stub
		return null;
	}

	public long getIndex(T focus, Page<T> page) {
		// TODO Auto-generated method stub
		return 0;
	}

	public String getPkNameByEntityClass(Class<?> entityClass) {
		// TODO Auto-generated method stub
		return null;
	}

	public Class<T> getEntityClass() {
		// TODO Auto-generated method stub
		return null;
	}

	public T save(T entity, String propertyList) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public T update(T entity, String propertyList) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public T get(T entity, String propertyList) {
		// TODO Auto-generated method stub
		return null;
	}

	public T getById(PK id, String propertyList) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findByExample(T entity, String propertyList, String orderBy, String propertyList2, long... firstAndMaxResults) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findByProperty(String propertyName, Object value, String orderBy, String propertyList) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findByProperty(String propertyName, Object value, String orderBy, String propertyList, long... firstAndMaxResults) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public <E> List<E> findByProperty(Class<E> entityClass, String propertyName, Object value, String orderBy, String propertyList, long... firstAndMaxResults) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, List<? extends Object>> getRefListMap(Class<?> objectClass, String aliasNames, Object entityPo) throws ClassNotFoundException, Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public int replaceRef(Class<?> objectClass, String aliasNames, Serializable srcId, Serializable tarId) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean isPersistentProperty(String propertyName) {
		// TODO Auto-generated method stub
		return false;
	}

	public <E> boolean isPersistentProperty(Class<E> clazz, String propertyName) {
		// TODO Auto-generated method stub
		return false;
	}

	public Map<PK, T> getInherentEntities() {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<Class<?>, Map<Serializable, Object>> loadProperties(Object entityPo, Class<?> entityClass, String propertyList,
			Map<Class<?>, Map<Serializable, Object>> propertyValueCache) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isPropertyNonNull(Class<?> entityClass, String propertyName) {
		// TODO Auto-generated method stub
		return false;
	}

	public <E> Map<Class<?>, Map<Serializable, Object>> loadProperties(Collection<E> list, Class<E> clazz, String propertyList,
			Map<Class<?>, Map<Serializable, Object>> propertyValueCache) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Map<String, Class<?>> getForeignKeyMap() {
		// TODO Auto-generated method stub
		return null;
	}

} 
